package com.dmcb.trade.controller;

import com.dmcb.common.business.utils.*;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.common.business.utils.WebClientUtil;
import com.dmcb.trade.business.crawlers.article.BaseParser;
import com.dmcb.trade.business.entities.*;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import com.dmcb.trade.business.services.ArticleCrawlerService;
import com.dmcb.trade.business.utils.CrawlerUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 文章爬取、解析
 * Created by long on 2016/3/14.
 */
@Controller
public class ArticleCrawlerController extends BaseCrawlerController {
    Logger logger = LoggerFactory.getLogger(ArticleCrawlerController.class);
    @Autowired
    ArticleCrawlerService articleCrawlerService;
    @Autowired
    private WebClientUtil webClientUtil;
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;



    /**
     * 解析文章、保存至正版库
     */
    @RequestMapping(value = "/article/trade/{authorId}", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult crawl(@PathVariable int authorId, String link) {
        return super.crawl(authorId, link, false, false);
    }

    /**
     * 解析文章、保存至监测库
     */
    @RequestMapping(value = "/article/trade/monitor/{userId}", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult crawlMonitor(@PathVariable int userId, String link) {
        return super.crawl(userId, link, true, false);
    }

    /**
     * 解析文章、返回内容
     */
    @RequestMapping(value = "/article/body/trade", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String crawlBody(String link) {
        if (link.startsWith("http://toutiao.com/")) {
            link = link.replaceAll("http://toutiao.com/", "http://www.toutiao.com/");
        }
        int tag = CrawlerUtil.tag(link);
        if (tag == -1) {
            return "";
        }
        String body = articleCrawlerService.parserMap.get(tag).parseBody(link);
        return body;
    }

    /**
     * 解析文章、返回内容
     */
    @RequestMapping(value = "/article/doc/trade", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String crawlDoc(String link) {
        int tag = CrawlerUtil.tag(link);
        if (tag == -1) {
            return "";
        }
        return articleCrawlerService.parserMap.get(tag).getDocument(link).toString();
    }

    /**
     * 解析文章、返回标题 时间
     */
    @RequestMapping(value = "/article/doc/crawlArticle", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult articleInfo(String link) {
        if(StringUtils.isBlank(link)){
            return outputJSONError("链接为空");
        }
        int tag = CrawlerUtil.tag(link);
        if (tag == -1) {
            return outputJSONError("该网站未支持： "+link);
        }
        return articleCrawlerService.parserMap.get(tag).parse(link);
    }


    @RequestMapping(value = "/change")
    public String change( ModelMap model) {
        int size = 1;
        String json;
        for (; size < 10; size++) {
            json = webClientUtil.getDoc("http://www.toutiao.com/a6438931951205220866/").toString();
            if (StringUtils.isBlank(json) || json.length() < 200) {
                RedisUtils.changeIP(15);
            } else {
                logger.info("总共替换了" + size + " 次");
                break;
            }
        }
        model.put("result", "ip重置成功，耐心等待一会,次数" + size);
        return "piracy/authorCrawler";

    }

    /**
     * 解析文章、返回内容
     */
    @RequestMapping(value = "/article/webutil/doc/trade", produces = "application/json; charset=utf-8")
    public String crawlWebUtilDoc(String link, ModelMap model) {
        link = StringUtils.isBlank(link) ? "http://www.toutiao.com/a6438931951205220866/" : link;
        String info = " 第一次使用代理就成功了，webClientUtil.getDoc(link)，不用加载头条主页";
        Document doc;
        String json = null;
        try {
            WebClientUtil.wc = webClientUtil.initWc(false, false, true);
            doc = webClientUtil.getDoc(link);
            if(doc == null){
                    info = "webClientUtil.getDoc(link)不行，尝试使用post请求";
                    json = WebUtil.postRequest(link, true,null);
                    if (StringUtils.isBlank(json) || json.length() < 400) {
                        info = "WebUtil.postRequest(link, true)没有获得数据，尝试使用本地post请求";
                        json = WebUtil.postRequest(link, false,null);
                        if (StringUtils.isBlank(json) || json.length() < 400) {
                            info = "WebUtil.postRequest(link, false)不行，尝试使用Webclient，加载头条主页";
                            WebClientUtil.wc = webClientUtil.initWc(false, false, true);
                            webClientUtil.getDoc("http://www.toutiao.com/");
                            doc = webClientUtil.getDoc(link);
                            json = doc.text();
                            if (StringUtils.isBlank(json) || json.length() < 400) {
                                info = "使用代理webClientUtil.getDoc(link),加载头条主页不行，尝试本地";
                                WebClientUtil.wc = webClientUtil.initWc(true, false, false);
                                webClientUtil.getDoc("http://www.toutiao.com/");
                                doc = webClientUtil.getDoc(link);
                                json = doc.text();
                            }
                        }
                    }

            }
            assert doc != null;
            json = doc.toString();
        } catch (Exception e) {
            logger.info(e.toString());
        }
        model.put("result", "如果后面没有文章内容，说明还是失败了，替换一次IP！页面--->" + json);
        model.put("info", info);
        return "piracy/authorCrawler";
    }


    /**
     * 解析文章、返回内容
     */
    @RequestMapping(value = "/article/body/parse", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String parseBody(String link, String xml) {
        int tag = CrawlerUtil.tag(link);
        if (tag == -1) {
            return "";
        }
        try {
            return articleCrawlerService.parserMap.get(tag).parseBodyFromXml(xml);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    //微信解析文章
    public JsonResult weChatCrawler(MonitorWechat monitorWechat, String url, BaseParser baseParser) throws Exception {
        return baseParser.parseWeChatMonitor(monitorWechat, url);
    }


    public JsonResult douBanCrawlMonitor(int authorId, String link) {
        JsonResult jsonResult = null;
        try {
            jsonResult = call(authorId, link, false, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonResult;
    }


    @RequestMapping(value = "/author/link/trade")
    @ResponseBody
    public JsonResult authorLinkCrawl(Integer size) {
        List<AuthorLink> authorLinks = articleCrawlMapper.queryTouTiaoForAuto(size);
        if (ListUtil.isEmpty(authorLinks)) {
            return outputJSONError("无待更新作者");
        }
        return outputJSONSucc(authorLinks);
    }

    @RequestMapping(value = "/query/author/link/trade")
    @ResponseBody
    public JsonResult authorLinkCrawlByAuthorId(Integer id) {
        List<AuthorLink> authorLinks = articleCrawlMapper.queryTouTiaoForAuthorId(id);
        if (ListUtil.isEmpty(authorLinks)) {
            return outputJSONError("无待更新作者");
        }
        return outputJSONSucc(authorLinks);
    }


    @RequestMapping(value = "/author/link/updatetime")
    @ResponseBody
    public JsonResult updateLinkLastTime(String link){
       return outputJSONSucc(articleCrawlMapper.updateLinkLastTime(link));
    }


    @RequestMapping(value = "/author/article/add")
    @ResponseBody
    public JsonResult addArticle(Integer userId,String title,String doc,String authorName,String coverPath,String link, String time) {
        if(userId == null){
            return outputJSONError("没有用户ID");
        }
        doc =doc.trim();
        String body = HtmlUtil.getTextFromHtml(doc);
        // 非监测，保存内容
        Article article = new Article();
        article.setAuthorName(authorName);
        article.setApproveStatus(userId);
        article.setAuthorId(userId);
        article.setCoverPath(coverPath);
        article.setPostTime(DateUtil.parseDate(time));
        article.setTitle(title);
        article.setLink(link);
        Body b = new Body(body);
        articleCrawlMapper.insertBody(b);
        // 设置内容ID、内容
        article.setBodyId(b.getId());
        article.setBody(HtmlUtil.getTextFromHtml(body));
        Author author = articleCrawlMapper.queryAuthor(userId);
        if(author == null){
            return outputJSONError("没有该作者");
        }
        Article article1 = articleCrawlMapper.queryByAuthorTitle(userId, article.getTitle());
        if (article1 != null) {

            return outputJSONSucc(0);
        }
        int tag = CrawlerUtil.tag(article.getLink());
        if(tag == -1){
            return outputJSONError("根据链接获取平台失败");
        }
        BaseParser baseParser = articleCrawlerService.parserMap.get(tag);
        baseParser.save(author,article,baseParser.getPlatForm());
        return outputJSONSucc(1);
    }



    /**
     * 调用爬取链接
     *
     * @param authorId 作者ID
     * @param link     链接
     * @return 消息
     */
    @Override
    protected JsonResult call(int authorId, String link, boolean monitor, boolean all) throws Exception {
        int tag = CrawlerUtil.tag(link);
        if (tag == -1) {
            return outputJSONError("目前该平台不支持解析，请联系快版客服人员处理，谢谢");
            /*return outputJSONError("目前仅支持天天快报、微信、头条、简书、搜狐、生命时报、网易、新浪、凤凰资讯、腾讯新闻、百家号、新华网、" +
                    "央视网、环球网、参考消息网、人民网、百度百家、中国政府网、大公网、中国青年网、央广网、国际在线网、" +
                    "星岛环球网、东方网、安庆新闻网、枞阳在线、华夏时报、千龙新闻网、新京报网、天津网、重庆晚报、东南网、" +
                    "梅州网、广西新闻网、贵州都市报、齐鲁晚报、河北青年网、石家庄新闻网、邯郸新闻网、河北新闻网、东方今报网、" +
                    "驻马店新闻网、漯河网、大河网、界面网、独家、钛媒体、砍柴网、36氪、投资界、虎嗅、艾瑞网、壹心理、TOPYS、" +
                    "人人都是产品经理、A5创业网、果壳网、中国诗歌网、书啦圈网、UE纵坐标、品玩、金融界、南方周末、每经网、" +
                    "X历史、GQ男士网、h韩星、南都娱乐、自媒网、京华时报、中国企业家网、金羊网、" +
                    "共识网、创业邦、中国经营网、商学院、职场、亿邦动力、推酷、悦旅行、传送门、拓扑日报、生活志、文艺生活网、新浪博客、" +
                    "中细软、中国行业信息网、商路通、商务服务网、51搜了网、陈军知识产权律师团队、中华取名网、法邦网、尚标网、一站阅读、" +
                    "那角落、北京联盟、酷饭网、论文网、个人图书馆、牛宝宝网、牛哥网、3023网、QKZZ网、流行社网、简笔画文章网、羊城晚报、" +
                    "作家网、爱尼微网、雨露文章网、语文迷、学、风尚网、打字酷酷网、三联网、圈圈网、就爱阅读、疾风网、" +
                    "学优网、百度文库、三亿文库、上海亚螺精密紧固技术有限公、中国大学生在线网、考试吧、烟草中国网、KJSON网、贵阳网、" +
                    "徐州在线、道州网、南阳事、炫6网、家长帮、亲亲宝贝、北方网、泡泡网、豆瓣网、爱微帮51自学网、靠垫儿、第一网贷、和讯网、" +
                    "人工智能中文资讯平台、站长之家、青网、慧博投研资讯、天涯、通信世界、北京参考、健康生活网、育儿网、万花镜、倍儿亲、" +
                    "铜梁视窗、品途、中国电子商务研究中心、乐读、开心、微头条、好酷网、散文网、一点资讯、粉丝网、果酷网、华职人才、商友圈、" +
                    "中国高校心里在线、闻康网、河北远程教育、一米阳光、经典散文网、人生指南网、21CN焦点、微时代、北京晚报、智悲教育、毕友网、" +
                    "乐读网、中国娱乐网、大嘴巴巴、涨姿势、新蓝网、煤炭网、即墨信息港、光明网、奥一网、长江网、蓝鲸网、加速会、片刻、犀牛故事网、" +
                    "马蜂窝网、长江日报、中关村在线网、时光网、澎湃网、创之网 、爱范儿导入，请重新输入链接地址");*/
        }
        BaseParser baseParser = articleCrawlerService.parserMap.get(tag);
        return monitor ? baseParser.parseMonitor(authorId, link) : baseParser.parse(authorId, link);
    }

}
